(function () {
	
    //未绑定table数据显示
    var serverData = {};
    var	serverTable;
    
    //已绑定table数据显示
    var serverData1 = {};
    var	serverTable1;
    serverData1.gradingId = '';
    
    //funnel
    var funnel;
    var funnelIndex = 0;
    jQuery(document).ready(function () {
        serverTable=  jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url": window.rootPath+'/ui/gradinguser/getUserByNotGrading',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [{
	                data: null,
	                defaultContent: ""
        			},
        			{
                    data: 'id',
                    render: function (data, type, full, meta) {
                        var operate ='<input type="checkbox" dataId="'+data+'" group="addDate"/>';
                        return operate;
                    }
    	        		},
                {
        				data: 'name'
                },
                {
                		data: 'hierarchy',
                		render: function(data, type, full, meta){
                			if(data){
                				return data.groupName;
                			}else{
                				return '';
                			}
                		}
                },
                {
                		data: 'grading',
                		render: function(data, type, full, meta){
                			if(data){
                				return data.gradingName;
                			}else{
                				return '';
                			}
                		}
                },
                {
                    data:null,
                    defaultContent: "",
                    render: function(data, type, full, meta){
                        var operate = '<button type="button" class="btn btn-sm btn-info addNode" title="绑定"><i class="fa fa-sign-in"></i></button>';
                        return operate;

                    }
                }],
            "drawCallback": function ( oSettings ) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for ( var i=oSettings._iDisplayStart, iLen=total ; i<iLen ; i++ )
                {
                    $('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[k] ].nTr ).text( i+1 );
                    k++;
                }

                serverTable.off('click','tr');
                serverTable.on( 'click', 'tr', function (event) {
                    event=event||window.event;
                    var target=event.target||event.scrElement;
                    var data = serverTable.row( this ).data();

                    if(target.className.indexOf('fa-sign-in')!=-1||target.className.indexOf('addNode')!=-1){
                    		setGrading(data,data.name);
                    }
                    return;
                } );
            }
        });
        
        serverTable1 = jQuery('#dataTable1').DataTable({
	        	"serverSide": true,
	        	"ordering": false,
	        	"ajax": {
	        		"dataType": "json",
	        		"type": "get",
	        		"cache": false,
	        		"url": window.rootPath+'/ui/gradinguser/getUserByGrading',
	        		"data": function (d) {
	        			return $.extend({}, d, serverData1);
	        		}
	        	},
	        	columns: [{
	        		data: null,
	        		defaultContent: ""
	        	},
	        	{
                data: 'id',
                render: function (data, type, full, meta) {
                    var operate ='<input type="checkbox" dataId="'+data+'" group="removeDate"/>';
                    return operate;
                }
        		},
	        	{
	        		data: 'name'
	        	},
	        	{
	        		data: 'hierarchy',
	        		render: function(data, type, full, meta){
	        			if(data){
	        				return data.groupName;
	        			}else{
	        				return '';
	        			}
	        		}
	        	},
	        	{
	        		data: 'grading',
	        		render: function(data, type, full, meta){
	        			if(data){
	        				return data.gradingName;
	        			}else{
	        				return '';
	        			}
	        		}
	        	},
	        	{
	        		data:null,
	        		defaultContent: "",
	        		render: function(data, type, full, meta){
	        			var operate = '<button type="button" class="btn btn-sm btn-info delBtn" title="移除"><i class="fa fa-remove"></i></button>';
	        			return operate;
	        			
	        		}
	        	}],
	        	"drawCallback": function ( oSettings ) {
	        		var k = 0;
	        		var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
	        		
	        		for ( var i=oSettings._iDisplayStart, iLen=total ; i<iLen ; i++ )
	        		{
	        			$('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[k] ].nTr ).text( i+1 );
	        			k++;
	        		}
	        		
	        		serverTable1.off('click','tr');
	        		serverTable1.on( 'click', 'tr', function (event) {
	        			event=event||window.event;
	        			var target=event.target||event.scrElement;
	        			var data = serverTable1.row( this ).data();
	        			
	        			if(target.className.indexOf('fa-remove')!=-1 || target.className.indexOf('delBtn')!=-1){
	        				
	        				removeGrading(data,data.name);
	        			}
	        			return;
	        		} );
	        	}
        });
        
        //未绑定查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#search').val();
            serverTable.draw();
        })
        
        //未绑定撤销查询
        jQuery('.resetBtn').on("click",function(){
            jQuery('#search').val('');
            serverData.key ='';
            serverTable.draw();
        });
        
        //已绑定查询
        jQuery('.searchBtn1').on('click',function(){
	        	serverData.key=jQuery('#search').val();
	        	serverTable.draw();
        })
        
        //已绑定撤销查询
        jQuery('.resetBtn1').on("click",function(){
	        	jQuery('#search1').val('');
	        	serverData1.key ='';
	        	serverTable1.draw();
    		});
        
        funnel.on('click', function (params) {
        		serverData1.gradingId = params.data.id;
        		serverTable1.draw();
        		showDownplay(funnelIndex);
        		funnelIndex = params.dataIndex;
        		showHighlight(funnelIndex);
        });
        
    });
    
    
    //未绑定 全选
    $("#allNotBound").click(function() {
    		var addDate = $(":checkbox[group=addDate]");
        if ($(this).prop("checked")) {
        		addDate.each(function() {
                $(this).prop("checked", true);  
            });  
        } else {  
        		addDate.each(function() {  
                $(this).prop("checked", false);  
            });  
        }  
    }); 
    
    //已绑定 全选
    $("#allBound").click(function() {
	    	var removeDate = $(":checkbox[group=removeDate]");
	    	if ($(this).prop("checked")) {
	    		removeDate.each(function() {
	    			$(this).prop("checked", true);  
	    		});  
	    	} else {  
	    		removeDate.each(function() {  
	    			$(this).prop("checked", false);  
	    		});  
	    	}  
    }); 
    
    //获取全选数据
    function getSelectData(group){
    		var obj = $(":checkbox[group="+group+"]:checked");
    		var myArray = new Array();
    		var i =0;
    		obj.each(function() {
    			myArray[i++] = $(this).attr('dataId');
    		});
    		return myArray;
    }
    
    //绑定全部信息
    jQuery('.addBtn').on('click', function() {
		var id = serverData1.gradingId;
		if(id == '' || id == '0'){
			alert("请选择评分层级!");
			return;
		}
	    	var dataInfo = getSelectData('addDate');
	    	if(dataInfo.length == 0){
	    		alert('请选择需要绑定的人员信息!');
			return;
	    	}
	    	var d = {
	    			"Id" : id,
	    			"dataInfo" : dataInfo
	    		};
	    	var url = window.rootPath +'/ui/gradinguser/addDataAll';
        jQuery.ajax({
            url:encodeURI(url),
            cache:false,
            type :"post",
            data: JSON.stringify(d),
            dataType:"json",
            contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
            success:function(data){
            		$("#allNotBound").prop("checked", false); 
            		$("#allBound").prop("checked", false); 
				alert(data.msg);
				serverTable.draw(false);
				serverTable1.draw(false);
            }
        });
    });
    
    //移除全部信息
    jQuery('.deleteBtn').on('click', function() {
    		var dataInfo = getSelectData('removeDate');
		if(dataInfo.length == 0){
			alert('请选择需要移除的人员信息!');
			return;
		}
	    	var d = {
	    			"dataInfo" : dataInfo
	    		};
	    	var url = window.rootPath +'/ui/gradinguser/removeDataAll';
	    	jQuery.ajax({
	    		url:encodeURI(url),
	    		cache:false,
	    		type :"post",
	    		data: JSON.stringify(d),
	    		dataType:"json",
	    		contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
	    		success:function(data){
	    			$("#allNotBound").prop("checked", false); 
            		$("#allBound").prop("checked", false); 
	    			alert(data.msg);
	    			serverTable.draw(false);
				serverTable1.draw(false);
	    		}
	    	});
    });
    

    //设置区域
    function setGrading(data,name){
    		if(serverData1.gradingId == ''){
    			alert('请选择需要绑定的评分区域!');
    			return;
    		}
        $.springLemonDialog({
            title:'系统提示',//标题   默认为   系统提示
            content:'是否要绑定'+name+'？',//确认文本
            bootstrapModalOption:{keyboard: true},
            clickButton:function(sender,modal,index){//按钮事件处理
                //index:0 确认    1：取消
                //sender:事件源 如按钮，sender.html()可得到按钮文字如：取消
                if(index==0){
                    var url=window.rootPath+'/ui/gradinguser/addUser';
                    var d = {
                            "gradingId":serverData1.gradingId,
                            "id":data.id
                        };
                    jQuery.ajax({
                        url:encodeURI(url),
                        cache:false,
                        type :"post",
                        data: JSON.stringify(d),
                        dataType:"json",
                        contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
                        success:function(data){
                            serverTable.draw(false);
                            serverTable1.draw(false);
                        }
                    });
                }
                $(this).closeDialog(modal);
            }
        });
    }
    
    
    function removeGrading(data,name){
    	$.springLemonDialog({
    		title:'系统提示',//标题   默认为   系统提示
    		content:'是否要移除'+name+'？',//确认文本
    		bootstrapModalOption:{keyboard: true},
    		clickButton:function(sender,modal,index){//按钮事件处理
    			//index:0 确认    1：取消
    			//sender:事件源 如按钮，sender.html()可得到按钮文字如：取消
    			if(index==0){
    				var url=window.rootPath+'/ui/gradinguser/removeUser';
    				var d = {
    						"id":data.id
    				};
    				jQuery.ajax({
    					url:encodeURI(url),
    					cache:false,
    					type :"post",
    					data: JSON.stringify(d),
    					dataType:"json",
    					contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
    					success:function(data){
    						serverTable.draw(false);
    						serverTable1.draw(false);
    					}
    				});
    			}
    			$(this).closeDialog(modal);
    		}
    	});
    }
    
    
    //funnel demo
    funnel = echarts.init(document.getElementById('funnel'));
    
    var option = {

            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b}"
            },
//            toolbox: {
//                feature: {
//                    dataView: { readOnly: false },
//                    restore: {},
//                    saveAsImage: {}
//                }
//            },
//            legend: {
//                data : ['领导','主任','组长']
//            },
            calculable: false,
            series: [{
                name: '评分层级',
                type: 'funnel',
                left: '1%',
                bottom: '5%',
                //x2: 80,

                width: '98%',
                //height: {totalHeight} - y - y2,
                min: 0,
                max: 2,
                minSize: '0%',
                maxSize: '100%',
                sort: 'ascending',//ascending descending
                gap: 2,
                label: {
                    normal: {
                        show: true,
                        position: 'inside'
                    },
                    emphasis: {
                        textStyle: {
                            fontSize: 18
                        }
                    }
                },
                labelLine: {
                    normal: {
                        length: 10,
                        lineStyle: {
                            width: 1,
                            type: 'solid'
                        }
                    }
                },
                itemStyle: {
                    normal: {//正常情况显示
                        borderColor: '#fff',
                        borderWidth: 1
                    },
                emphasis:{//高亮情况显示
                    borderColor: '#f00',
                    borderWidth: 4,
                    }
                },
                data: ''

            }]
        };

    
    funnel.setOption(option);
    
    function updateFunnel(data){
    		funnel.setOption({
    			series:{
    				max:data.max,
    				data :data.data
    			}
        });
    }
    
    jQuery.ajax({
        url:window.rootPath+'/ui/gradinguser/getGrading',
        cache:false,
        type :"get",
        data:'',
        dataType:"json",
        contentType:'application/json;charset=utf-8',//解决返回数据中的中文乱码问题
        success:function(data){
        		updateFunnel(data);
        }
    });
    function showDownplay(index){
	    // 取消之前高亮的图形
	    funnel.dispatchAction({
	        type: 'downplay',
	        seriesIndex: 0,
	        dataIndex: index
	    });
    }
    function showHighlight(index){
	    // 高亮当前图形
	    funnel.dispatchAction({
	        type: 'highlight',
	        seriesIndex: 0,
	        dataIndex: index
	    });
    }
})();